<?php

namespace aerovuelos\FrontBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\Common\Collections\ArrayCollection;
use aerovuelos\VueloBundle\Entity\Vuelos;
use aerovuelos\ReservaBundle\Entity\Reservas;
use aerovuelos\PersonaBundle\Entity\Personas;
use aerovuelos\ReservaBundle\Entity\ReservasXPersonas;
use aerovuelos\FrontBundle\Form\BuscarVuelosType;
use aerovuelos\FrontBundle\Form\ReservaVuelosUnoType;
use aerovuelos\FrontBundle\Form\ReservaVuelosDosType;
use aerovuelos\ReservaBundle\Form\ReservasXPersonasType;

class FrontController extends Controller {

    public function inicioAction() {
        return $this->render('FrontBundle:Front:inicio.html.twig', array(
        ));
    }

    public function vuelosAction() {
        $em = $this->getDoctrine()->getManager();


        $entity = new Vuelos();
        $form = $this->createForm(new BuscarVuelosType(), $entity);

        $vuelos = $em->getRepository('VueloBundle:Vuelos')->find(0);


        $localidades = $em->getRepository('UbicacionBundle:Localidades')->findAll();

        return $this->render('FrontBundle:Front:vuelos.html.twig', array(
                    'form' => $form->createView(),
                    'localidades' => $localidades,
                    'vuelos' => $vuelos,
        ));
    }

    public function buscarVuelosAction() {
        $em = $this->getDoctrine()->getManager();

        $request = $this->getRequest();

        $entity = new Vuelos();
        $form = $this->createForm(new BuscarVuelosType(), $entity);
        $form->bind($request);

        $origen = $form->get('origen')->getData();
        $destino = $form->get('destino')->getData();
        $fechaSalida = $form->get('fechaSalida')->getData();


        $vuelos = $em->getRepository('VueloBundle:Vuelos')->findVuelos($origen, $destino, $fechaSalida);


        $localidades = $em->getRepository('UbicacionBundle:Localidades')->findAll();

        return $this->render('FrontBundle:Front:vuelos.html.twig', array(
                    'form' => $form->createView(),
                    'localidades' => $localidades,
                    'vuelos' => $vuelos,
        ));
    }

    public function reservaUnoAction($id_vuelo) {
        $em = $this->getDoctrine()->getManager();

        $vuelo = $em->getRepository('VueloBundle:Vuelos')->find($id_vuelo);

        $request = $this->getRequest();

        $persona = new Personas();
        $form = $this->createForm(new ReservaVuelosUnoType(), $persona);

        if ($request->getMethod() == 'POST') {
            $form->bind($request);

            $tipoPersona = $em->getRepository('PersonaBundle:TipoPersona')->find(1);
            $persona->setTipoPersona($tipoPersona);
            $em->persist($persona);


            $reserva = new Reservas();

            $reserva->setPersonas($persona);
            $reserva->setVuelos($vuelo);

            $reserva->setCodigoReserva(md5(time()));

            $em->persist($reserva);


            $em->flush();

            $cantPers = $form->get('cantPers')->getData();

            return $this->redirect($this->generateUrl('front_reservar_vuelo_dos', array('id_res' => $reserva->getId(), 'cantPers' => $cantPers, 'count' => 0)));
        }

        return $this->render('FrontBundle:Front:reserva.html.twig', array(
                    'form' => $form->createView(),
                    'vuelo' => $vuelo,
        ));
    }

    public function reservaDosAction($id_res, $cantPers, $count) {
        $em = $this->getDoctrine()->getManager();

        $request = $this->getRequest();

        $persona = new Personas();
        $form = $this->createForm(new ReservaVuelosDosType(), $persona);

        if ($request->getMethod() == 'POST') {
            $form->bind($request);

            $em->persist($persona);

            $asiento = $form->get('asiento')->getData();

            $reserva = $em->getRepository('ReservaBundle:Reservas')->find($id_res);

            $reservaXPersona = new ReservasXPersonas();

            $reservaXPersona->setPersonas($persona);
            $reservaXPersona->setReservas($reserva);
            $reservaXPersona->setAsiento($asiento);

            $em->persist($reservaXPersona);



            if ($count < ($cantPers - 1 )) {
                $em->flush();

                return $this->redirect($this->generateUrl('front_reservar_vuelo_dos', array('id_res' => $id_res, 'cantPers' => $cantPers, 'count' => ($count + 1))));
            } else {
                $reservaXPersona = $em->getRepository('ReservaBundle:ReservasXPersonas')->findBy(array(
                    'reservas' => $id_res
                ));


                $monto = 0;
                foreach ($reservaXPersona as $res) {
                    $tipo_pers = $res->getPersonas()->getTipoPersona()->getNombre();

                    if ($tipo_pers == 'Bebes') {
                        $monto = $monto + $res->getReservas()->getVuelos()->getPrecioBebes();
                    } elseif ($tipo_pers == 'Niños') {
                        $monto = $monto + $res->getReservas()->getVuelos()->getPrecioNinios();
                    } else {
                        $monto = $monto + $res->getReservas()->getVuelos()->getPrecioAdulto();
                    }
                }


                $reserva->setPrecioTotal($monto);
                $em->persist($reserva);


                $em->flush();

                return $this->redirect($this->generateUrl('front_reservar_vuelo_tres', array('id_res' => $id_res)));
            }
        }

        return $this->render('FrontBundle:Front:reserva2.html.twig', array(
                    'form' => $form->createView(),
                    'id_res' => $id_res,
                    'cantPers' => $cantPers,
                    'count' => $count
        ));
    }

    public function reservaTresAction($id_res) {
        $em = $this->getDoctrine()->getManager();

        $reserva = $em->getRepository('ReservaBundle:Reservas')->find($id_res);
        $reservaXPersona = $em->getRepository('ReservaBundle:ReservasXPersonas')->findBy(array(
            'reservas' => $id_res
        ));

        return $this->render('FrontBundle:Front:reserva3.html.twig', array(
                    'reserva' => $reserva,
                    'reservasXPersona' => $reservaXPersona
        ));
    }

    public function reservaExitosaAction($id_res) {
        $em = $this->getDoctrine()->getManager();

        $reserva = $em->getRepository('ReservaBundle:Reservas')->find($id_res);

        return $this->render('FrontBundle:Front:reservaExitosa.html.twig', array(
                    'reserva' => $reserva
        ));
    }

    public function reservaPagoExitosoAction($id_res) {
        $em = $this->getDoctrine()->getManager();

        $reserva = $em->getRepository('ReservaBundle:Reservas')->find($id_res);

        $reservaXPersona = $em->getRepository('ReservaBundle:ReservasXPersonas')->findBy(array(
            'reservas' => $id_res
        ));

        $reserva->setCompraEfectiva(1);

        $em->persist($reserva);

        foreach ($reservaXPersona as $res) {
            $res->setCodigoEmbarque(md5(time()));

            $em->persist($res);
        }

        $em->flush();


        return $this->render('FrontBundle:Front:reservaPagoExitoso.html.twig', array(
                    'reservasXPersonas' => $reservaXPersona
        ));
    }

    public function reservaPagoAction($id_res) {
        $em = $this->getDoctrine()->getManager();

        $reserva = $em->getRepository('ReservaBundle:Reservas')->find($id_res);

        return $this->render('FrontBundle:Front:reservaPago.html.twig', array(
                    'reserva' => $reserva
        ));
    }

    public function reservaPagoValidarCodigoAction() {
        $em = $this->getDoctrine()->getManager();
        
        $request = $this->getRequest();
        
        $codigoRes = $request->query->get('codRes');
        
        $reserva = $em->getRepository('ReservaBundle:Reservas')->findOneBy(array(
            'codigoReserva' => $codigoRes
        ));
        
        if($reserva){
             return $this->redirect($this->generateUrl('front_reservar_vuelo_pago', array('id_res' => $reserva->getId())));
        }

        return $this->render('FrontBundle:Front:reservaPago.html.twig', array(
                    'reserva' => $reserva
        ));
    }

    public function pagosAction() {
        return $this->render('FrontBundle:Front:pagos.html.twig');
    }

    public function ofertasAction() {
        return $this->render('FrontBundle:Front:ofertas.html.twig');
    }

    public function contactoAction() {
        return $this->render('FrontBundle:Front:contacto.html.twig');
    }

    public function nosotrosAction() {
        return $this->render('FrontBundle:Front:nosotros.html.twig');
    }

}
